#include "sysinfo.h"

#include <QFile>
#include <QProcess>
#include <QDebug>

Sysinfo::Sysinfo() = default;

/**
 * @brief Sysinfo::getUploadway 获取上行网络接口
 * @return
 */
QString Sysinfo::getUploadway()
{
    QString cmdRlz;
    QProcess p(nullptr);

    p.start("/bin/sh -c \"ip route show | grep ^default | head -n 1 | awk \'{print $5}\'\"");
    p.waitForStarted();
    p.waitForFinished();
    cmdRlz = p.readAllStandardOutput();
    if (cmdRlz == "eth0\n")
    {
        cmdRlz = "以太网";
    }
    else if (cmdRlz == "3g-4G\n")
    {
        cmdRlz = "4G";
    }
    else
    {
        cmdRlz = "无";
    }

    p.close();

    return cmdRlz;
}

QString Sysinfo::getUploadIface()
{
    QString cmdRlz;
    QProcess p(nullptr);

    p.start("/bin/sh -c \"ip route show | grep ^default | head -n 1 | awk \'{print $5}\'\"");
    p.waitForStarted();
    p.waitForFinished();
    cmdRlz = p.readAllStandardOutput();
    if (cmdRlz == "eth0\n")
    {
        cmdRlz = "eth0";
    }
    else if (cmdRlz == "3g-4G\n")
    {
        cmdRlz = "4G";
    }
    else
    {
        cmdRlz = nullptr;
    }

    p.close();

    return cmdRlz;
}

/**
 * @brief Sysinfo::getLocation 获取定位信息
 * @return
 */
QString Sysinfo::getLocation()
{
    QByteArray line;
    QFile file(LOCATION_FILE);

    if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
    {
        return line;
    }

    line = file.readLine();

    file.close();
    return line;
}

/**
 * @brief Sysinfo::getVersion 获取系统版本信息
 * @return
 */
QString Sysinfo::getVersion()
{
    QByteArray verstr;
    QByteArray line;
    QFile file(VERSION_FILE);

    if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
    {
        return verstr;
    }

    while (!file.atEnd())
    {
        line = file.readLine();
        if (line.startsWith("DISTRIB_RELEASE="))
        {
            verstr = line.mid(17, line.length() - 17 - 2);
            break;
        }
    }

    file.close();

    return verstr;
}

QString Sysinfo::getIpAddr(const QString &iface)
{
    QString cmdRlz, cmdstr;

    if (iface.isEmpty())
    {
        return nullptr;
    }

    QProcess p(nullptr);
    cmdstr += "/bin/sh -c \"";
    cmdstr += "ip -f inet addr show ";
    cmdstr += iface;
    cmdstr += " | grep inet | grep -oE \"[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}/[0-9]{1,2}\" \"";

    p.start(cmdstr);
    p.waitForStarted();
    p.waitForFinished();
    cmdRlz = p.readAllStandardOutput();


    p.close();

    return cmdRlz;
}

QString Sysinfo::getQdSrvInfo()
{
    QString cmdRlz, cmdstr;
    QProcess p(nullptr);

    cmdstr += "/bin/sh -c \"";
    cmdstr += "/usr/bin/qdsrvinfo";

    p.start(cmdstr);
    p.waitForStarted();
    p.waitForFinished();
    cmdRlz = p.readAllStandardOutput();

    p.close();

    return cmdRlz;
}

int Sysinfo::getBackLight()
{
    QString cmdRlz, cmdstr;
    QProcess p(nullptr);

    cmdstr = "cat ";
    cmdstr += BACKLIGHT_CTRL;

    p.start(cmdstr);
    p.waitForStarted();
    p.waitForFinished();
    cmdRlz = p.readAllStandardOutput();

    p.close();

    return cmdRlz.toInt();
}

void Sysinfo::setBackLight(int level)
{
    char cmdstr[64];

    if (level < 1 || level > 4)
    {
        qDebug() << "level invalid";
        return;
    }

    QProcess p(nullptr);

    snprintf(cmdstr, sizeof(cmdstr) - 1, "echo %d > %s", level, BACKLIGHT_CTRL);
    system(cmdstr);
}
